/**
 * 
 */
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
import org.w3c.dom.Document;

/**
 * <b>Description:</b><br/> It reads the saved mapping either from the file or from the 
 * 		'elementMap' data-structure and stores it in the 'crosswalk' hashtable. It provides the
 * 		flexibility to the user to directly specify the crosswalk in a file (according to a format)
 * 		and use it to map source schema XML instances to RIF-CS XML without having to parse the 
 * 		source schema XSD to generate one half of the crosswalk. 
 *   
 * 
 * <br/><br/><b>Last modified:</b><br/> 25th July 2011	
 * 
 * @author Sunny Toor
 * @param n/a
 * @return n/a
 * @exception n/a
 * 
 */
public class ReadSavedMapping {

	/** crosswalk containing the mapping from source schema to RIFCS schema  */
	private Hashtable<String, String> crosswalk;
	
	/** parsed schema of the source schema XSD */
	private ParseSchema schema = new ParseSchema();
	
	/**
	 * <b>Description:</b><br/> constructor to instantiate the class and initialise crosswalk
	 * 
	 * <br/><br/><b>Last modified:</b><br/> 25th July 2011
	 * 
	 * @author Sunny Toor
	 * @param n/a
	 * @return n/a
	 * @exception n/a
	 * 
	 */
	public ReadSavedMapping() {
		// TODO Auto-generated constructor stubd
		crosswalk = new Hashtable<String,String>();
	}

	/**
	 * <b>Description:</b><br/> Reads in the file containing the crosswalk (according to a specific format) 
	 * 		and stores all the information extracted into <i>'crosswalk'</i> data-structure. 
	 * 
	 * <br/><br/><b>Last modified:</b><br/> 25th July 2011
	 * 
	 * @author Sunny Toor
	 * @param Name of the file
	 * @return n/a
	 * @exception File IO Exception
	 * 
	 */
	private void constructCrosswalkFromFile(String filename)throws Exception{
		BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
		while(in.ready()){
			String oneMapping = in.readLine();
			String[] keyValuePair = oneMapping.split(" -> ");
			if(keyValuePair.length == 2){
				this.crosswalk.put(keyValuePair[0], keyValuePair[1]);
			}	
		}
		in.close();
		
	}
	
	/**
	 * <b>Description:</b><br/> Constructs the crosswalk from the 'elementMap' (from ParseSchema.java)
	 * 		in case the file (crosswalk.txt) is unavailable. 
	 * 
	 * <br/><br/><b>Last modified:</b><br/> 25th July 2011
	 * 
	 * @author Sunny Toor
	 * @param n/a
	 * @return n/a
	 * @exception File IO Exception
	 * 
	 */
	private void constructDefaultCrossWalk() throws Exception{
		Document d = schema.getDocReprensation("mods-schema.xml");
		schema.traverseDoc(d, null);
		this.crosswalk = schema.getElementMap();
	}
	
	/**
	 * <b>Description:</b><br/> Returns the crosswalk constructed either from 
	 * 		constructDefaultCrossWalk() or constructCrosswalkFromFile() depending upon 
	 * 		whether or not the file exists. 
	 * 
	 * <br/><br/><b>Last modified:</b><br/> 25th July 2011
	 * 
	 * @author Sunny Toor
	 * @param name of the file
	 * @return crosswalk 
	 * @exception File IO Exception
	 * 
	 */
	public Hashtable<String,String> getCrossWalk(String filename)throws Exception{
		File fn = new File(filename);
		if(!(fn.exists() || filename.isEmpty())){
			this.constructDefaultCrossWalk();
		}
		else{
			this.constructCrosswalkFromFile(filename);
		}
		return this.crosswalk;
	}
	
	/**
	 * <b>Description:</b><br/> Test method that displays the crosswalk on the screen.
	 * 
	 * <br/><br/><b>Last modified:</b><br/> 25th July 2011
	 * 
	 * @author Sunny Toor
	 * @param crosswalk and the output stream
	 * @return n/a
	 * @exception n/a
	 * 
	 */
	public void printCrossWalk(Hashtable<String,String> crossWalk, PrintStream out){
		if(crossWalk.isEmpty()){
			out.println("No elements read from file.");
		}
		else{
			for(Enumeration<String> e = crossWalk.keys(); e.hasMoreElements();){
				String k = e.nextElement();
				out.println(k+" :- "+crossWalk.get(k));
			}
		}
	}
	
	/**
	 * <b>Description:</b><br/> Main method that drives the whole class
	 * 
	 * <br/><br/><b>Last modified:</b><br/> 25th July 2011
	 * 
	 * @author Sunny Toor
	 * @param args
	 * @return n/a
	 * @exception File IO Exception
	 * 
	 */
	public static void main(String[] args) throws Exception{
		ReadSavedMapping rs = new ReadSavedMapping();
		Hashtable<String,String> h = rs.getCrossWalk(args[0]);
		rs.printCrossWalk(h,System.out);

	}

}
